package com.dx.architectureddd.adapter.web;

import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dx.architectureddd.api.dto.req.ScheduledTaskForm;
import com.dx.architectureddd.common.task.ScheduledTasks;
import com.dx.architectureddd.common.task.config.ScheduledTaskRegistrar;
import com.dx.architectureddd.infrastructure.impl.SysUser;
import com.dx.architectureddd.infrastructure.impl.SysUserService;
import com.dx.architectureddd.infrastructure.impl.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务前端控制器
 *
 * @author xin.deng
 * @date 2023/10/14
 */
@Slf4j
@RestController
@RequestMapping("/biz")
public class BizController {

    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysUserService userService;

    @PostMapping("/task")
    public SingleResponse<Long> taskExecute(@RequestBody ScheduledTaskForm form) {
        ScheduledTasks tasks = new ScheduledTasks(() -> {
            log.info("task id:{}, is run, cron:{}, time:{}", form.getTaskId(), form.getCronStr(), new Date());
            // 实际业务
        }, form.getTaskId(), form.getCronStr());
        ScheduledTaskRegistrar.addScheduledTask(tasks);
        return SingleResponse.of(form.getTaskId());
    }

    @GetMapping("/userList")
    public SingleResponse<List<SysUser>> list() {
        return SingleResponse.of(userMapper.selectList(new QueryWrapper<>()));
    }

    @GetMapping("/add")
    public SingleResponse<SysUser> add() {
        SysUser sysUser = new SysUser();
        sysUser.setUserName("测试");
        sysUser.setUserStatus(1);
        sysUser.setUserSex(1);
        sysUser.setUserPassword("231313");
        sysUser.setUserIdcardType(1);
        sysUser.setUserPhone("2132312312");
        boolean b = userService.saveOrUpdate(sysUser);
        log.info("是否成功：{}", b);
        return SingleResponse.of(sysUser);
    }
}
